package com.itheima.easy.adapter.impl;

import com.itheima.easy.adapter.PagePayAdapter;
import com.itheima.easy.constant.TradingConstant;
import com.itheima.easy.handler.PagePayHandler;
import com.itheima.easy.utils.RegisterBeanHandler;
import com.itheima.easy.vo.TradingVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PagePayAdapter.java
 * @Description PC网页支付适配实现
 */
@Slf4j
@Component
public class PagePayAdapterImpl implements PagePayAdapter {

    @Autowired
    RegisterBeanHandler registerBeanHandler;

    @Autowired
    RedissonClient redissonClient;

    private static Map<String,String> pagePayHandlers =new HashMap<>();

    static {
        pagePayHandlers.put(TradingConstant.TRADING_CHANNEL_ALI_PAY,"aliPagePayHandler");
        pagePayHandlers.put(TradingConstant.TRADING_CHANNEL_WECHAT_PAY,"wechatPagePayHandler");
    }

    /***
     * @description 生成交易表单，渲染后自动跳转支付宝或者微信引导用户完成支付
     * @param tradingVo 订单单
     * @return TradingVo对象中的placeOrderJson：支付宝为form表单字符串、微信为prepay_id标识
     */
    public TradingVo pageTrading(TradingVo tradingVo) {
        //根据渠道获得bean名称
        String pagePayHandlerString = pagePayHandlers.get(tradingVo.getTradingChannel());

        //从IOC容器中找到PagePayHandler实现
        PagePayHandler pagePayHandler = registerBeanHandler.getBean(pagePayHandlerString, PagePayHandler.class);

        //payTrade支付交易处理
        TradingVo tradingVoResult = pagePayHandler.pageTrading(tradingVo);
        return tradingVoResult;
    }
}
